package com.zj.leetcode.medium.greed;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @program: algorithm
 * @description:
 * 任务调度器
 * 621 task-scheduler
 * @author: Zhang Bo
 * @create: 2022-03-21 11:06:55
 **/
public class TaskScheduler{
    public static void main(String[] args) {
        Solution solution = new TaskScheduler().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int leastInterval(char[] tasks, int n) {

            Map<Character, Integer> freq = new HashMap<Character, Integer>();
            // 最多的执行次数
            int maxExec = 0;
            for (char ch : tasks) {
                int exec = freq.getOrDefault(ch, 0) + 1;
                freq.put(ch, exec);
                maxExec = Math.max(maxExec, exec);
            }

            // 具有最多执行次数的任务数量
            int maxCount = 0;
            Set<Map.Entry<Character, Integer>> entrySet = freq.entrySet();
            for (Map.Entry<Character, Integer> entry : entrySet) {
                int value = entry.getValue();
                if (value == maxExec) {
                    ++maxCount;
                }
            }

            return Math.max((maxExec - 1) * (n + 1) + maxCount, tasks.length);

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
